package com.wlcorp.forcazamis.sys.permission;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.sys.util.SessionValue;

@SuppressWarnings("unchecked")
public class PermissionManager {

  private static PermissionManager permissionManager;
  private PermissionMap permissionMap;
  private PermissionManager() {
    permissionMap = PermissionXMLBuilder.createPermissionMap();
  }
  public static PermissionManager getInstance(){
    if(permissionManager == null){
      permissionManager = new PermissionManager();
    }
    return permissionManager;
  }

  public List getPermissionGroupList(){
    return permissionMap.getPermissionGroups();
  }
  public List getPermissionPagesByGroup(String topGroupId){
    return permissionMap.getPermissionPagesByTopGroup(topGroupId);
  }

  public List getPermissionPageList(){
    return permissionMap.getPermissionPages();
  }

  public List getSubPermissionList(String permissionPageId){
    return permissionMap.getPermissionSubList(permissionPageId);
  }

  public boolean checkPermission(String[] permissionDef,String permissionId){
    if(null == permissionDef || permissionDef.length == 0){
      return false;
    }
    for(int i = 0;i < permissionDef.length;i++){
      if(permissionId.equals(permissionDef[i])){
        return true;
      }
    }
    return false;
  }

  public boolean checkGroupPermission(String[] permissionDef,String action){
    String groupId = getPermissionPageIdByAction(action);
    if("-1".equals(groupId)){
      return true;
    }
    return checkPermission(permissionDef,groupId);
  }

  public String checkSubPermission(String groupId,HttpServletRequest request){
    //AddinCache addinCache = (AddinCache)request.getSession().getAttribute(WebNameConsts.ADDIN_CACHE);
	Staff staff = SessionValue.getInstance(request).getStaff();
    String[] permissionDef = staff.getPermission();
    StringBuffer resultStr = new StringBuffer("<script>\n");
    List subPermissionList = getSubPermissionList(groupId);
    for(Iterator iter = subPermissionList.iterator();iter.hasNext();){
      PermissionSub permissionSub = (PermissionSub)iter.next();
      String paramId = permissionSub.getId();
      String type = permissionSub.getType();
      String value = permissionSub.getValue();
      boolean isEnumerated = permissionSub.isEnumerated();
      boolean hasPermission = checkPermission(permissionDef,paramId);
      if(!hasPermission){
    	if(isEnumerated){
    		resultStr.append("var elementArray = document.getElementsByTagName('" + type + "');\n");
    		resultStr.append("for(i = 0;i < elementArray.length;i++){\n");
    		resultStr.append("if(elementArray[i].id=='" + value + "'){elementArray[i].style.display='none';}\n");
    		resultStr.append("}\n");
    	}else{
    		resultStr.append("if(document.getElementById('" + value + "')){document.getElementById('" + value + "').disabled=true;}\n");
    	}
      }
    }
    resultStr.append("</script>\n");
    return resultStr.toString();
  }

  public String checkPermissionPageSub(String groupSelectName,HttpServletRequest request){
//    AddinCache addinCache = (AddinCache)request.getSession().getAttribute(WebNameConsts.ADDIN_CACHE);
//    String groupId = addinCache.getUser().groupId.trim();
//    List managedGroupList = addinCache.getUser().getManagedGroupList();
//    if(OBConstants.SYS_ADMINISTRATOR.equals(groupId)){
//      return "";
//    }
//    StringBuffer strBuf = new StringBuffer("<script language='javascript'>\n");
//    strBuf.append("var managedGroupArray = new Array();\n");
//    strBuf.append("var userGroupId = '").append(groupId).append("';\n");
//    for(Iterator iter = managedGroupList.iterator();iter.hasNext();){
//      Map groupMap = (Map)iter.next();
//      String groupIdA = (String)groupMap.get("GROUP_INFO_ID_A");
//      strBuf.append("managedGroupArray[managedGroupArray.length] = '").append(groupIdA.trim()).append("';\n");
//    }
//    strBuf.append("var select = document.getElementById('").append(groupSelectName).append("');\n");
//    strBuf.append("var options = select.options;\n");
//    strBuf.append("var newOptions = new Array();\n");
//    strBuf.append("var option;\n");
//    strBuf.append("for(i = 0;i < options.length;i++){\n");
//    strBuf.append("if(options[i].value == userGroupId){\n");
//    strBuf.append("option = document.createElement(\"OPTION\");\n");
//    strBuf.append("option.value = options[i].value;\n");
//    strBuf.append("option.text = options[i].text;\n");
//    strBuf.append("newOptions[newOptions.length] = option;\n");
//    strBuf.append("continue;\n");
//    strBuf.append("}\n");
//    strBuf.append("for(index = 0;index < managedGroupArray.length;index++){\n");
//    strBuf.append("if(options[i].value == managedGroupArray[index]){\n");
//    strBuf.append("option = document.createElement(\"OPTION\");\n");
//    strBuf.append("option.value = options[i].value;\n");
//    strBuf.append("option.text = options[i].text;\n");
//    strBuf.append("newOptions[newOptions.length] = option;\n");
//    strBuf.append("break;\n");
//    strBuf.append("}\n");
//    strBuf.append("}\n");
//    strBuf.append("}\n");
//    strBuf.append("select.options.length = 0;\n");
//    strBuf.append("for(i = 0;i < newOptions.length;i++){\n");
//    strBuf.append("select.add(newOptions[i]);\n");
//    strBuf.append("}\n");
//    strBuf.append("</script>\n");
//    return strBuf.toString();
	  return null;
  }

  private String getPermissionPageIdByAction(String action){
    List permissionPageList = getPermissionPageList();
    String permissionPageId = "-1";
    for(Iterator iter = permissionPageList.iterator();iter.hasNext();){
      PermissionPage permissionPage = (PermissionPage)iter.next();
      if(permissionPage.getAction().equals(action)){
    	  permissionPageId = permissionPage.getId();
        break;
      }
    }
    return permissionPageId;
  }


}
